The Java programming language has been developed to facilitate the development of programs that can be executed on a wide variety of computers, independent of the computers' particular system or processor architecture. The Java programming language is generally object-oriented, and a program developer generates a Java program in the form of one or more classes. A Java compiler ("javac") compiles the program into Java byte code ("JBC") for distribution. When the Java program is to be executed on a computer, the Java bye code can be executed in an interpretive manner by a Java "virtual machine" which is provided on the computer. Alternatively, the Java byte code can be compiled to the native instruction code of the computer on which the Java program is to be executed by, for example, a "just-in-time" ("JIT") compiler. The just-in-time compiler provides the compiled code to the Java virtual machine for execution. In processing the Java program, the Java virtual machine provides a number of services, including interfacing with the computer's operating system to, for example, obtain data ("input") for processing and provide processed data ("output") for storage, display to an operator and so forth. The Java programming language generally lacks the "struct" ("structure") concept. In programming languages such as "C", the structure concept allows a programmer to define a new data type comprising a collection of one or more variables identified by a data type identifier. After the structure has been defined, the programmer can easily establish, or "declare" a new variable having the data type's structure by referencing the identifier for the structure in the variable's declaration. As is conventional, a program (more specifically, a process or thread) has memory structures including one or more stacks and a heap in which data can be stored, and in programming languages such as "C" the programmer can declare the new variable to be established on a stack or allocate space for it in the program's heap. While programming languages such as "C" provides for a significant degree of flexibility to a programmer, a problem can arise, most particularly if the variable is established in the heap, as follows. If a variable has been declared and used in a program, and if space for it has been allocated on the heap, the programmer will generally need to deallocate the space if the variable is not going to be used thereafter, otherwise the space will continue to be allocated for the variable, which can reduce the space available for variable which are declared subsequently during processing of the program.
As noted above, the Java programming language lacks the concept of a "structure." In Java, in order to achieve a similar effect as that provided by a "structure" in "C," a Java programmer must resort to usage of a degenerate class that contains fields, but no methods. A Java program may use a degenerate class, which is also referred to as a "degenerate container class," by enabling an object to be created therefor by use of a "new" operator. An example of the use of the "new" operator will be clear from Code Segments 1A and 1B
Code Segment 1A class aPoint { //degenerate class int x ; int y ; int zplane ; public void scale ( int Factor) //method to scale this.x and this.y { this.x *= Factor ; this.y *= Factor ; } public void add ( int x, int y ) //method to increment this.x and this.y { this.x += x ; this.y += y ; } } Code Segment 1B class body { public int UsePoint ( int x, int y, int Q) { int temp, dist ; aPoint pt = new aPoint () ; //object "pt" created using aPoint tmp = (x &lt; y) ? x : y ; pt.x = x - 5 ; //translate pt.y = y - 10 ; if (tmp &lt; 100) { pt.x *= 2 ; //scale pt.y *= 2 ; } dist = (int) Math.sqrt ( (double) (pt.x * pt.x) + (double) (pt.y * pt.y) ) ; if (tmp &lt; 0) { dist = -dist ; } return dist ; } }
In Code Segment 1A, a degenerate container class aPoint is defined, which is used in Code Segment 1B. The degenerate container class aPoint defines variables aPoint.x, aPoint.y and aPoint.zplane and two methods, namely, a method Scale and a method add. In Code Segment 1B, a class "body" is defined having a single method body.UsePoint which makes use of the degenerate container class aPoint to define a new variable "pt" having components aPoint.x (established as pt.x) and aPoint.y (established as pt.y). In the method body.UsePoint, various operations to be performed in connection with the variable "pt," including translation, conditional scaling and distance determination, and provides a single return value "dist." During execution, in response to the "new" operator in method body.UsePoint, a new object for the degenerate container class aPoint is established on the heap for the program which is using body.UsePoint. The variable aPoint.zplane in the degenerate container class aPoint is not used in body.UsePoint, but it would be instantiated as part of the object aPoint in response to the "new" operator in body.UsePoint.
Several problems arise in connection with object variables, such as that described above in connection with aPoint, which are established on the heap. First, the object includes not only room for the aPoint variables, but also object header information, which can require a fairly extensive amount of storage. In addition, as noted above, in programming languages such as "C," the programmer explicitly deallocates space on the heap that is allocated to variable which will no longer be needed. Such explicit deallocation is not performed in connection with programs written in the Java programming language. Instead, the Java virtual machine contains a "garbage collection" mechanism which monitors the usage of variables and other objects for which space is allocated in the heap and, if it determines that an object is not likely to be used in the future, it will deallocate the space automatically. Garbage collection is not needed in connection with variables, termed "method variables" or "method fields," which are allocated on the stack, since, when the method which uses the variables returns, the entire portion of the stack (the "stack frame") used during execution of the method, which will include the variables which were used during the execution of the method, will automatically be eliminated without requiring intervention of the garbage collection mechanism. However, in Java, only simple field variables, such as method variables may be allocated on the stack, not composite types such as structures available in "C". Garbage collection requires use of the computer's processor, which takes resources that it (that is, the processor) might otherwise devote to other operations, and typically the larger the heap, the more resources the processor will need to devote to garbage collection. In addition, as suggested above, the object variables that are declared in a degenerate container class will be instantiated as part of an object even if they are not used in the method for which the object is instantiated, which can waste storage space in the heap.